package com.siyuanren.gcocp.web;

import com.siyuanren.gcocp.annotation.ValidCheck;
import com.siyuanren.gcocp.common.exception.BaseException;
import com.siyuanren.gcocp.common.util.ValidUtils;
import com.siyuanren.gcocp.common.web.bean.Result;
import com.siyuanren.gcocp.dto.ChapterDTO;
import com.siyuanren.gcocp.entity.WxUserChapter;
import com.siyuanren.gcocp.entity.WxUserVideoProgress;
import com.siyuanren.gcocp.service.ChapterService;
import com.siyuanren.gcocp.util.VaildUtil;
import com.siyuanren.gcocp.vo.*;
import com.siyuanren.gcocp.web.wx.BaseWxController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * Created by   anxing
 * Project_name gcocp-parent
 * Path         com.siyuanren.gcocp.web
 * Date         2018/4/18
 * Time         13:39
 * Description 课程内容controller
 */
@Slf4j
@Controller
@RequestMapping("/chapter")
public class ChapterController extends BaseWxController {
    @Autowired
    ChapterService chapterService;

    /**
     * 返回章节列表
     * @param queryChapterVO
     * @return
     *
     */
    @ResponseBody
    @RequestMapping("chapterList")
    public Result getChapterList(QueryChapterVO queryChapterVO) throws Exception {
        VaildUtil.check(queryChapterVO);
        queryChapterVO.setUserId(getUserId());
        List<ChapterDTO> chapterList = chapterService.getChapterList(queryChapterVO);
        return new Result(chapterList);
    }

    /**
     * 返回章节详情
     * @param chapterDetailVO
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("getChapterDetail")
    public Result getChapterDetail(QueryChapterDetailVO chapterDetailVO) throws Exception {
        VaildUtil.check(chapterDetailVO);
        chapterDetailVO.setUserId(getUserId());
        ChapterDTO dto = chapterService.getChapterDetail(chapterDetailVO);
        return new Result(dto);
    }
    /**
     * 返回课程打卡列表
     * @param queryChapterVO
     * @return
     */
    @ResponseBody
    @RequestMapping("clockList")
    public Result getCourseClockList(QueryChapterVO queryChapterVO) throws Exception {
        VaildUtil.check(queryChapterVO);
        queryChapterVO.setUserId(getUserId());
        List<Integer> courseClockList = chapterService.getCourseClockList(queryChapterVO);
        return new Result(courseClockList);
    }

    /**
     * 上报章节学习进度
     * @param userChapter
     * @return
     */
    @ResponseBody
    @RequestMapping("submitChapterProgress")
    public Result submitChapterCourse(WxUserChapter userChapter) throws Exception {
        VaildUtil.check(userChapter);
        userChapter.setUserId(getUserId());
        userChapter.setStudyTime(new Date());
        chapterService.submitChapterCourse(userChapter);
        return new Result();
    }

    /**
     * 返回学习进度
     * @param chapterId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("getStudyProgress")
    public Result getStudyProgress(Integer chapterId) throws Exception {
        if (chapterId == null) {
            throw new BaseException("章节id不能为空");
        }
        Integer lastContentId = chapterService.getStudyProgress(getUserId(), chapterId);
        return new Result(lastContentId);
    }

    /**
     * 课程打卡
     * @param clockChapterVO
     */
    @ResponseBody
    @RequestMapping("clock")
    public Result addCourseClock(ClockChapterVO clockChapterVO) throws Exception {
        VaildUtil.check(clockChapterVO);
        clockChapterVO.setUserId(getUserId());
        chapterService.addCourseClock(clockChapterVO);
        return new Result();
    };

    @ResponseBody
    @RequestMapping("addPlayCount")
    public Result addPlayCount(AddPlayCountVO addPlayCountVO) throws Exception {
        VaildUtil.check(addPlayCountVO);
        chapterService.addPlayCount(addPlayCountVO);
        return new Result();
    }

    /**
     * 提交视频播放进度
     * @param vo
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("submitVideoProgress")
    public Result submitVideoProgress(VideoProgoressVO vo) throws Exception {
        VaildUtil.check(vo);
        vo.setUserId(getUserId());
        chapterService.submitVideoProgress(vo);
        return new Result();
    }
}
